
import javafx.animation.AnimationTimer;
import javafx.event.EventHandler;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.ArrayList;

public class GameScene {
    AnchorPane anchorPane;
    Canvas canvas=new Canvas(SuperMarioBros.Width,SuperMarioBros.Height);
    GraphicsContext graphicsContext=canvas.getGraphicsContext2D();
    Refresh refresh=new Refresh();
    MarioBros mb=new MarioBros(260,480,Fx.stop,this);
    Background background=new Background();
    KeyProcess keyProcess=new KeyProcess();
    public Sprite brick,well,gold,mushroom;
    public static ArrayList<Sprite> list=new ArrayList();
    //加载地图配置
    String[][] map=null;
    {
        Map mp=new Map();
        map=mp.DuQuMap();
        CunChuDiTu();
    }
    //绘制角色，地图，怪物，
    public void pain(){
        background.pain(graphicsContext);
        mb.pain(graphicsContext);
        for (int i = 0; i < list.size(); i++) {
            Sprite sp=list.get(i);
            sp.pain(graphicsContext);

        }
        graphicsContext.setFont(new Font(20));
        graphicsContext.fillText("时间：" + "时间", 800, 60);
        graphicsContext.fillText("金币:" +"金币", 800, 90);


    }
    //初始化游戏
    public void ini(Stage stage){
        anchorPane=new AnchorPane(canvas);
        stage.getScene().setRoot(anchorPane);
        refresh.start();
        stage.getScene().setOnKeyReleased(keyProcess);
        stage.getScene().setOnKeyPressed(keyProcess);

    }
    //便利二维数组，创建障碍物，并且把他们存入list
    public void CunChuDiTu(){
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if (map[i][j].equals("1")){
                    brick=new Brick(new Image("brick.png"),j*40,i*40,40,40);
                    list.add(brick);
                }
                if (map[i][j].equals("2")){
                    well=new Well(new Image("coin_brick.png"),j*40,i*40,40,40);
                    list.add(well);
                }
                if (map[i][j].equals("3")){
                    gold=new Gold(new Image("ob6.png"),j*40,i*40,40,40);
                    list.add(gold);
                }
                if (map[i][j].equals("4")){
                    mushroom=new Mushroom(new Image("mogu.png"),j*40,i*40,40,40);
                    list.add(mushroom);
                }

            }
        }
    }
    //这个刷新界面
    private class Refresh extends AnimationTimer{

        @Override
        public void handle(long now) {
            pain();
        }
    }
    //移动事件监听
 private class KeyProcess implements EventHandler<KeyEvent>{


     @Override
     public void handle(KeyEvent event) {
         KeyCode keyCode=event.getCode();
         if (event.getEventType()== KeyEvent.KEY_RELEASED){
             mb.SongKai(keyCode);
         }else if (event.getEventType()== KeyEvent.KEY_PRESSED){
             mb.AnXia(keyCode);
         }




     }
 }
 //角色死亡调用
 public static void GameOver(){
     System.out.println("111");
 }
}
